from datetime import datetime

# 给定的日期字符串
sdate = '2024-04-15'

# 将字符串解析为日期对象
date_obj = datetime.strptime(sdate, '%Y-%m-%d')

# 定义一个字典来映射星期几的索引到中文
weekday_map = {
    0: '星期一',
    1: '星期二',
    2: '星期三',
    3: '星期四',
    4: '星期五',
    5: '星期六',
    6: '星期日'
}

# 获取星期几的索引并映射到中文
weekday_chinese = weekday_map[date_obj.weekday()]

print(weekday_chinese)  # 输出: 星期一

import pandas as pd

# 原始数据
datas = [
    {"condition": "SDEmpmd", "humidity": 610.221, "predict_date": "2025-04-18", "temp_high": 659.991, "temp_low": 333.065, "weather_day": "HwSTMDpaw", "wind_dir_day": "mUnbvqWB", "wind_dir_night": "mQqCS", "wind_level_day": "LNfqEucyBMl", "wind_level_night": "PXzQIMfiAMarEe"},
    {"condition": "nZCBLaBfPHY", "humidity": 735.235, "predict_date": "2025-04-16", "temp_high": 803.294, "temp_low": 865.185, "weather_day": "wTFTucNNXqA", "wind_dir_day": "AXNFRFWpGcQz", "wind_dir_night": "HiXHEEOxc", "wind_level_day": "HoTcwkxDe", "wind_level_night": "lfmhTMRACKmV"},
    {"condition": "JMcaxTyoXFKA", "humidity": 595.311, "predict_date": "2025-04-17", "temp_high": 543.465, "temp_low": 261.915, "weather_day": "UWObC", "wind_dir_day": "FkaSKW", "wind_dir_night": "ZAQjvptLsVjr", "wind_level_day": "xUJlpdpoqkRxkf", "wind_level_night": "xigweoAbFlfsJQ"}
]

# 转换为 DataFrame
df = pd.DataFrame(datas)

# 打印结果
print(df)

# 数据清洗
for data in datas:
    data['humidity'] = float(data['humidity'])
    data['temp_high'] = float(data['temp_high'])
    data['temp_low'] = float(data['temp_low'])

# 打印清洗后的数据
print(datas)

# 筛选数据
filtered_data = [data for data in datas if data['predict_date'] == "2025-04-17"]

# 打印筛选结果
print(filtered_data)

# 示例映射表
weather_mapping = {
    "HwSTMDpaw": "晴",
    "wTFTucNNXqA": "多云",
    "UWObC": "阴"
}

# 应用映射
for data in datas:
    data['weather_day'] = weather_mapping.get(data['weather_day'], "未知")

# 打印映射后的数据
print(datas)


from datetime import datetime
import pandas as pd

# 原始数据
datas = [
    {"condition": "SDEmpmd", "humidity": 610.221, "predict_date": "2025-04-18", "temp_high": 659.991, "temp_low": 333.065, "weather_day": "HwSTMDpaw", "wind_dir_day": "mUnbvqWB", "wind_dir_night": "mQqCS", "wind_level_day": "LNfqEucyBMl", "wind_level_night": "PXzQIMfiAMarEe"},
    {"condition": "nZCBLaBfPHY", "humidity": 735.235, "predict_date": "2025-04-16", "temp_high": 803.294, "temp_low": 865.185, "weather_day": "wTFTucNNXqA", "wind_dir_day": "AXNFRFWpGcQz", "wind_dir_night": "HiXHEEOxc", "wind_level_day": "HoTcwkxDe", "wind_level_night": "lfmhTMRACKmV"},
    {"condition": "JMcaxTyoXFKA", "humidity": 595.311, "predict_date": "2025-04-17", "temp_high": 543.465, "temp_low": 261.915, "weather_day": "UWObC", "wind_dir_day": "FkaSKW", "wind_dir_night": "ZAQjvptLsVjr", "wind_level_day": "xUJlpdpoqkRxkf", "wind_level_night": "xigweoAbFlfsJQ"}
]

# 定义一个字典来映射星期几的索引到中文
weekday_map = {
    0: '星期一',
    1: '星期二',
    2: '星期三',
    3: '星期四',
    4: '星期五',
    5: '星期六',
    6: '星期日'
}

# 添加 weekly 字段
for data in datas:
    # 解析 predict_date 字段为日期对象
    date_obj = datetime.strptime(data['predict_date'], '%Y-%m-%d')
    # 获取星期几的索引并映射到中文
    weekday_chinese = weekday_map[date_obj.weekday()]
    # 添加 weekly 字段
    data['weekly'] = weekday_chinese

# 打印结果
print(datas)

# 转换为 DataFrame
df = pd.DataFrame(datas)

# 打印 DataFrame
print(df)
